package com.winchain.chainbill.bill.biz.contract.service.impl;

import com.shell.bee.base.utils.StringUtil;
import com.shell.bee.contract.po.ContBas;
import com.shell.bee.contract.po.ContProt;
import com.shell.bee.contract.po.ContSign;
import com.shell.bee.contract.service.ContBasService;
import com.shell.bee.contract.service.ContProtService;
import com.shell.bee.contract.service.ContSignService;
import com.shell.bee.persistence.annotation.Pagination;
import com.shell.bee.persistence.model.PageLimit;
import com.winchain.chainbill.bill.base.po.CbRec;
import com.winchain.chainbill.bill.base.service.CbRecService;
import com.winchain.chainbill.bill.biz.contract.dao.ContractQueryDao;
import com.winchain.chainbill.bill.biz.contract.service.ContractQueryService;
import com.winchain.chainbill.bill.biz.contract.vo.ContQueryVo;
import com.winchain.chainbill.tbill.base.po.TbRec;
import com.winchain.chainbill.tbill.base.service.TbRecService;
import java.util.List;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("contractQueryService")
public class ContractQueryServiceImpl implements ContractQueryService {
	@Autowired
	private ContSignService contSignService;
	@Autowired
	private ContProtService contProtService;
	@Autowired
	private ContBasService contBasService;
	@Autowired
	private ContractQueryDao contractQueryDao;
	@Autowired
	private CbRecService cbRecService;
	@Autowired
	private TbRecService tbRecService;

	@Pagination
	public List<ContQueryVo> selectContBasForPage(PageLimit limit, ContQueryVo contQueryVo) {
		if (!StringUtil.emptyAndNull(contQueryVo.getSearchCode())) {
			StringUtil.getParameterLike(contQueryVo.getSearchCode());
		}
		if (!StringUtil.emptyAndNull(contQueryVo.getEndDate())) {
			String endDate = contQueryVo.getEndDate();
			contQueryVo.setEndDate(endDate + " 23:59:59");
		}
		if (!StringUtil.emptyAndNull(contQueryVo.getStartDate())) {
			String startDate = contQueryVo.getStartDate();
			contQueryVo.setStartDate(startDate + " 00:00:00");
		}
		if (!StringUtil.emptyAndNull(contQueryVo.getBizType())) {
			if (("15".equals(contQueryVo.getBizType())) || ("17".equals(contQueryVo.getBizType()))) {
				contQueryVo.setBizType("10");
			} else if ("16".equals(contQueryVo.getBizType())) {
				contQueryVo.setBizType("11");
			}
		}
		if (StringUtil.notEmpty(contQueryVo.getSearchCode())) {
			contQueryVo.setSearchCode(StringUtil.getParameterLike(contQueryVo.getSearchCode()));
		}
		return this.contractQueryDao.selectContBasForPage(contQueryVo);
	}

	public ContQueryVo selectContractDetail(String contId, String custId) {
		ContQueryVo result = new ContQueryVo();
		ContBas contBas = (ContBas) this.contBasService.selectByKey(contId);
		BeanUtils.copyProperties(contBas, result);
		result.setCustId(custId);
		ContSign cs = new ContSign();
		cs.setContId(contId);
		cs.setCustId(custId);
		ContSign scs = (ContSign) this.contSignService.selectOne(cs);
		result.setSignTime(scs.getSignTime());
		ContProt cp = new ContProt();
		cp.setContId(contId);
		CbRec cr = (CbRec) this.cbRecService.selectByKey(contBas.getBizId());
		if (cr != null) {
			result.setBillCode(cr.getBillCode());
		} else {
			TbRec tbRec = (TbRec) this.tbRecService.selectByKey(contBas.getBizId());
			if (tbRec != null) {
				result.setBillCode(tbRec.getBillCode());
			}
		}
		List<ContProt> cps = this.contProtService.selectByEntity(cp);
		if ((cps != null) && (cps.size() > 0)) {
			result.setProtId(((ContProt) cps.get(0)).getProtId());
		}
		return result;
	}
}
